<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登陆</title>
    <script src="/js/jquery-1.11.3.min.js"></script>
</head>
<body>
<h1>登陆</h1>
    <div>
        用户名：<input type="text" name="username" id="username" value="admin" autocomplete="false">
    </div>
    <div>
        密码：<input type="password" name="password" id="password">
    </div>
    <div>
        <button onclick="login()">立即登陆</button>
    </div>
<script type="application/javascript">
    function login() {
        $.ajax({
            url: "/login",
            type: "POST",
            data: {
              "username": $("#username").val(),
              "password": $("#password").val()
            },
            success: function(r, textStatus, request) {
                debugger
                // console.log(request);
                // console.log(request.getAllResponseHeaders());
                var token = request.getResponseHeader("token");  //关键在这里通过此方法获得参数
                console.log(token);
                if (token) {
                    localStorage.setItem('token', token);
                    location.href = "/data/home";
                } else {
                    alert('获取token失败！')
                }
            }
        })
    }

    $(function() {
        //全局的ajax访问，处理ajax清求时异常
        $.ajaxSetup({
            contentType:"application/x-www-form-urlencoded;charset=utf-8",
            global: true,
            beforeSend: function(xhr) {
                debugger
                xhr.setRequestHeader("Authorization", localStorage.getItem('token'));
            },
            success: function(result,status,xhr) {
            },
            complete:function(XMLHttpRequest,textStatus){
            }
        });
    })


</script>
</body>
</html>